package com.example.jl.bethinner.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.View;

import com.example.jl.bethinner.R;


/**
 * Created by PhChen on 2015/10/19.
 */
public class SDTableView extends View {

    private float width, height;                                  //画布长宽
    private float histogramWidth, histogramHeight;                //表格长宽
    private int valueY = 10;                                      //Y轴最大值
    private int valueX = 6;                                       //X轴最大值
    private float offsetYText = 0;                                //文字X轴偏移量
    private float offsetYTop = 10;
    private float offsetYBottom = 80;

    public int[] valueBarList = {300, 600, 500, 550, 400,800,900,460,870,1000,100, 600, 500, 550, 400,800,900,460,870,1000,100};        //条形图数据列表
    private int barWidth = 40, barHeight = 150;                   //条形长宽
    private int paddingWidth =barWidth/2;
    private int barX, barY;
    private int barOriginX=50;  //图表起点x坐标
    private boolean isFrist = true;
    private int MAXVALUE = 0;

    public SDTableView(Context context) {
        super(context);
    }

    public SDTableView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray = context.getTheme()
                .obtainStyledAttributes(
                        attrs,
                        R.styleable.CustomView
                        , 0, 0);

//        try {
////            mBorderColor = typedArray.getColor(R.styleable.CustomView_border_color, 0xff000000);
////            mBorderWidth = typedArray.getDimension(R.styleable.CustomView_border_width, 2);
//        } finally {
//            typedArray.recycle();
//        }
//        init();
    }


    public SDTableView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
//        init();
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        width = canvas.getWidth();
        height = canvas.getHeight();

        // 设置画布的背景颜色
        canvas.drawColor(Color.TRANSPARENT);
        /**
         * 定义矩形为空心
         */
        // 定义画笔1
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        // 消除锯齿
        paint.setAntiAlias(true);
        // 设置画笔的颜色
        paint.setColor(Color.GRAY);
        // 设置paint的外框宽度
        paint.setStrokeWidth(getResources().getDimension(R.dimen.dimen_1));

        // 定义画笔2
        Paint tPaint = new Paint();
        tPaint.setStyle(Paint.Style.STROKE);
        // 消除锯齿
        tPaint.setAntiAlias(true);
        // 设置画笔的颜色
        tPaint.setColor(Color.GRAY);
//        tPaint.setTextAlign(Paint.Align.CENTER);

        //调整字体位置
        Paint.FontMetrics fontMetrics = tPaint.getFontMetrics();
        float fontTotalHeight = fontMetrics.bottom - fontMetrics.top;
        offsetYText = fontTotalHeight / 2;
        //柱状图长宽
        histogramWidth = valueBarList.length* (barWidth+paddingWidth) + barOriginX;
        histogramHeight = height - offsetYTop - offsetYBottom;
        if (isFrist) {
            getBarHeight();
            isFrist = false;
        }

        //画出底部x，y轴
        for (int i = 0; i <= valueY; i++) {
            paint.setColor(Color.GRAY);
            tPaint.setColor(Color.GRAY);
            tPaint.setTextSize(25);
            canvas.drawText((valueY - i) *(2*MAXVALUE)/valueY+"", 0, offsetYText + offsetYTop + histogramHeight / valueY * i
                    , tPaint);
            canvas.drawLine(50, offsetYTop + histogramHeight / valueY * i, histogramWidth
                    , offsetYTop + histogramHeight / valueY * i, paint);

        }

        //横轴上的文字
        for (int i = 0; i < valueBarList.length; i++) {
            canvas.drawText(i + "月", i * (barWidth+paddingWidth) + barOriginX, height
                    , tPaint);
        }

        canvas.drawLine(0, height, width, height, paint);

        drawBar(canvas);
    }

    //画出条形图
    private void drawBar(Canvas canvas) {

        barX = barOriginX;
        float[] outerR = {barWidth / 2, barWidth / 2, barWidth / 2, barWidth / 2, 0, 0, 0, 0};
        float[] outerR2 = {0, 0, 0, 0, barWidth / 2, barWidth / 2, barWidth / 2, barWidth / 2};
        RoundRectShape rectShape = new RoundRectShape(outerR, null, null);
        ShapeDrawable mDrawables = new ShapeDrawable(rectShape);
//        //设置颜色渐变
//        Shader mShader = new LinearGradient(0, 0,barWidth, 0,
//                new int[] { Color.RED, Color.GREEN }, null, Shader.TileMode.REPEAT); // 一个材质,打造出一个线性梯度沿著一条线。
        int i = 0;
        for (int barHeight : valueBarList) {
            barY = (int) (offsetYTop + histogramHeight / 2) - barHeight;//
//            if (barHeight > 0) {
//                rectShape = new RoundRectShape(outerR, null, null);
//                mDrawables = new ShapeDrawable(rectShape);
//                mDrawables.setBounds(barX, barY, barX + barWidth, barY + barHeight);
//            } else {
//                rectShape = new RoundRectShape(outerR2, null, null);
//                mDrawables = new ShapeDrawable(rectShape);
//                mDrawables.setBounds(barX, barY + barHeight, barX + barWidth, barY);
//            }
            rectShape = new RoundRectShape(outerR, null, null);
            mDrawables = new ShapeDrawable(rectShape);
            mDrawables.setBounds(barX, barY, barX + barWidth, (int) histogramHeight);
//            mDrawables.getPaint().setARGB(255, 120, 205, 209);
            colorGradient(mDrawables.getPaint(),barHeight);
            i++;
            mDrawables.draw(canvas);
            barX +=  paddingWidth+ barWidth;
        }
    }

    //颜色渐变
    private void colorGradient(Paint paint,int barHeight) {
        Shader shader;
        int barHeightValue =getBarHeightValue(barHeight);
        if (barHeightValue<300){
            shader = new LinearGradient(0, 0, 200,barHeightValue,
                    new int[]{Color.YELLOW, Color.GREEN},
                    null, Shader.TileMode.MIRROR);
        }else if (barHeightValue<500){
            shader = new LinearGradient(0, 0, 200,barHeightValue ,
                    new int[]{Color.RED, Color.YELLOW, Color.GREEN},
                    null, Shader.TileMode.MIRROR);
        }else {
            shader = new LinearGradient(0, 0, 200,barHeightValue ,
                    new int[]{Color.RED, Color.YELLOW, Color.GREEN},
                    null, Shader.TileMode.MIRROR);
        }
                // 设置shader
        paint.setShader(shader);
    }

    //将实际数值转换为长度
    private void getBarHeight() {
        for (int i = 0; i < valueBarList.length; i++) {
            MAXVALUE = (MAXVALUE>valueBarList[i]?MAXVALUE:valueBarList[i]);
            valueBarList[i] = valueBarList[i] * (int) histogramHeight / (100 * valueY) - (int) histogramHeight / 2;
        }
    }
    private int getBarHeightValue(int barHeight) {
        return (barHeight+(int) histogramHeight / 2)/((int) histogramHeight / (100 * valueY));

    }
    public void setValueBarList(int[] valueBarList) {
        this.valueBarList = valueBarList;

    }
}
